﻿#ifndef UIDEVICEINFO_H
#define UIDEVICEINFO_H

#include <QWidget>
#include <model/device/uidevicemanage.h>
#include <model/device/uideviceinfocard.h>
#include <shlib/quc_sdk/customwidget/uicustomdialog.h>

#include <ElaMessageBar.h>
#include <ElaProgressBar.h>
#include <ElaComboBox.h>
#include <ElaLineEdit.h>
#include <ElaIconButton.h>
#include <ElaContentDialog.h>
#include <ElaText.h>
#include <ElaTabWidget.h>
#include <ElaPushButton.h>

class UiDeviceInfoEdit : public QWidget {
    Q_OBJECT
public:
    enum EEditFlag {
        EDeviceAdd,
        EDeviceChange,

    };

    UiDeviceInfoEdit(EEditFlag flag,CRtDeviceInfo*info, QStringList& groups,QWidget* parent =  nullptr):QWidget(parent),m_info(info), m_flag(flag){
        this->setWindowFlags(Qt::FramelessWindowHint);
        this->setStyleSheet("background-color: rgba(0, 0, 0, 0);");

        QVBoxLayout* vlayout = new QVBoxLayout;

        ElaText* lab_name = new ElaText("设备名字:", this);
        QFont font = lab_name->font();
        font.setPointSize(14);
        lab_name->setFont(font);
        lab_name->setFixedSize(100, 30);
        naemEdit = new ElaLineEdit(this);
        naemEdit->setFixedHeight(30);
        QHBoxLayout* naemLayout = new QHBoxLayout;
        naemLayout->setSpacing(30);
        naemLayout->addWidget(lab_name);
        naemLayout->addWidget(naemEdit);

        ElaText* lab_descr = new ElaText("设备描述:", this);
        lab_descr->setFont(font);
        lab_descr->setFixedSize(100, 30);
        descrEdit = new ElaLineEdit(this);
        descrEdit->setFixedHeight(30);
        QHBoxLayout* descrLayout = new QHBoxLayout;
        descrLayout->setSpacing(30);
        descrLayout->addWidget(lab_descr);
        descrLayout->addWidget(descrEdit);

        ElaText* lab_group = new ElaText("选择分组:");
        lab_group->setFixedSize(100, 30);
        lab_group->setFont(font);
        groupBox = new ElaComboBox(this);
        groupBox->setFixedHeight(30);
        groupBox->setEditable(true);
        groupBox->addItems(groups);
        QHBoxLayout* groupLayout = new QHBoxLayout;
        groupLayout->setSpacing(30);
        groupLayout->addWidget(lab_group);
        groupLayout->addWidget(groupBox);

        ElaText* lab_location = new ElaText("安装位置:");
        lab_location->setFont(font);
        lab_location->setFixedSize(100, 30);
        locationEdit = new ElaLineEdit;
        locationEdit->setFixedHeight(30);
        QHBoxLayout* locationLayout = new QHBoxLayout;
        locationLayout->setSpacing(30);
        locationLayout->addWidget(lab_location);
        locationLayout->addWidget(locationEdit);

        ElaText* lab_protocol = new ElaText("通信协议:");
        lab_protocol->setFixedSize(100, 30);
        lab_protocol->setFont(font);
        groupBoxProtocol = new ElaComboBox;
        QStringList protocols;
        protocols << "modbus-tcp" << "modbus-rtu" << "mqtt";
        groupBoxProtocol->addItems(protocols);
        groupBoxProtocol->setFixedHeight(30);
        QHBoxLayout* groupProtocolLayout = new QHBoxLayout;
        groupProtocolLayout->setSpacing(30);
        groupProtocolLayout->addWidget(lab_protocol);
        groupProtocolLayout->addWidget(groupBoxProtocol);

        ElaText* lab_version = new ElaText("固件版本:");
        lab_version->setFont(font);
        lab_version->setFixedSize(100, 30);
        descrversionEdit = new ElaLineEdit;
        descrversionEdit->setFixedHeight(30);
        QHBoxLayout* descrversionLayout = new QHBoxLayout;
        descrversionLayout->setSpacing(30);
        descrversionLayout->addWidget(lab_version);
        descrversionLayout->addWidget(descrversionEdit);

        ElaText* lab_valid = new ElaText("是否禁用:");
        lab_valid->setFont(font);
        lab_valid->setFixedSize(100, 30);
        validBox = new ElaComboBox;
        validBox->setFixedHeight(30);
        validBox->addItems(QStringList() << "启用" << "禁用");
        QHBoxLayout* validLayout = new QHBoxLayout;
        validLayout->setSpacing(30);
        validLayout->addWidget(lab_valid);
        validLayout->addWidget(validBox);

        vlayout->addLayout(naemLayout);
        vlayout->addLayout(descrLayout);
        vlayout->addLayout(groupLayout);
        vlayout->addLayout(locationLayout);
        vlayout->addLayout(groupProtocolLayout);
        vlayout->addLayout(descrversionLayout);
        vlayout->addLayout(validLayout);

        this->setLayout(vlayout);

        loadData();
    }

    bool loadData() {
        if(!m_info)
            return false;

        naemEdit->setText(m_info->name);
        descrEdit->setText(m_info->descr);
        groupBox->setCurrentText(m_info->grouping);
        locationEdit->setText(m_info->location);
        groupBoxProtocol->setCurrentText(m_info->protocol);
        descrversionEdit->setText(m_info->firmware_version);
        validBox->setCurrentIndex(m_info->valid ? 0 : 1);

        if(m_flag == EDeviceChange) {
            naemEdit->setEnabled(false);
        }

        return true;
    }

    bool save() {
        if(naemEdit->text().isEmpty() || descrEdit->text().isEmpty()) {
            return false;
        }

        m_info->name = naemEdit->text();
        m_info->descr = descrEdit->text();
        m_info->grouping = groupBox->currentText();
        m_info->location = locationEdit->text();
        m_info->protocol = groupBoxProtocol->currentText();
        m_info->valid = validBox->currentIndex() == 0 ? true : false;

        return true;
    }

protected:


private:
    CRtDeviceInfo* m_info = nullptr;

    ElaLineEdit* naemEdit = nullptr;
    ElaLineEdit* descrEdit = nullptr;
    ElaComboBox* groupBox = nullptr;
    ElaLineEdit* locationEdit = nullptr;
    ElaComboBox* groupBoxProtocol = nullptr;
    ElaLineEdit* descrversionEdit = nullptr;
    ElaComboBox* validBox = nullptr;

    EEditFlag m_flag;
};

class UiDeviceInfoProtocolEdit : public QWidget {
    Q_OBJECT
public:
    UiDeviceInfoProtocolEdit(QWidget* parent =  nullptr):QWidget(parent){

    }

private:
    CRtDeviceInfo* m_info = nullptr;
};


class UiDeviceInfo : public QWidget {
    Q_OBJECT
public:
    UiDeviceInfo(QWidget* parent = nullptr);
    void initUi();

signals:

public slots:
    void slotSelectModelChange(const QString& modelname, const QString& user);
    void addDevice();
protected:

public:
    class QVBoxLayout* m_mainLayout = nullptr;
    class ElaPushButton* m_newSceneBtn = nullptr;
    class ElaPushButton* m_refreshSceneBtn = nullptr;
    class PanelWidget* m_view = nullptr;

    QMap<QString, UiDeviceInfoCard*> m_deviceCardsMap;
    QString m_user;
    QString m_modelName;
};

#endif // UIDEVICEINFO_H
